<?php
/**
 * Created by PhpStorm.
 * User: chenzhi
 * Date: 16/8/18
 * Time: 下午7:21
 */


class Dcommentm extends CI_Model
{
    //专题评论列表缓存key
    const DCOMMENTLIST_ASC_KEY = 'dcomment_list_key_by_asc1_';


    function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取评论详情
     * @param int $comment_id
     */
    public function get_comment_info($comment_id = 0)
    {
        if (!$comment_id) return array();

        $result = $this->db->select('*')
            ->from('ml_disubject_comment')
            ->where('id', $comment_id)
            ->get();
        $result = $result->result_array();

        if (!isset($result[0])) return array();
        return $result[0];
    }


    //获取一个专题的评论列表
    public function get_comment_list($did, $start, $limit,$order_by)
    {
        ci_load_once('userm');
        ci_load_once('dcommentupm');
        $mc = $this->cache->memcached;
        $this->cache->memcached->is_supported();

//w_log('获取一个专题的评论列表');
        $key = self::DCOMMENTLIST_ASC_KEY . $did;
        $query = $mc->get($key);
        if (!$query) {
            //w_log('没有缓存');
            $this->db->select("ml_disubject_comment.*");
            $this->db->where('ml_disubject_comment.did', $did);
            $this->db->where('ml_disubject_comment.is_show', 1);
            $this->db->limit($limit, $start);
            if ($order_by) {
                foreach ($order_by as $k => $v) {
                    $this->db->order_by($k, $v);
                }
            }
            $query = $this->db->from("ml_disubject_comment")->get()->result_array();
            //w_log('sql is '.$this->db->last_query());
            $mc->save($key, $query, 60 * 60);
        }
        $return = [];
        if($query)
        {
            foreach ($query as $value)
            {
                $author_info = $this->userm->getUserInfo($value['author_id']);

                $author_info['author_name'] = $author_info['is_show_truename'] ? $author_info['truename'] : $author_info['username'];

                $value['author_info'] = $author_info;
                $value['commentup_count'] = $this->dcommentupm->get_commentup_count(array('comment_id'=>$value['id']));
                $return[] = $value;
            }
        }
        //w_log(json_encode($return));

        return $return;
    }


    //插入评论
    public function insert($data)
    {
        $this->db->insert("ml_disubject_comment",$data);
        //w_log('last_query is '.$this->db->last_query());
        $insert_comment_result =  $this->db->insert_id();
        return $insert_comment_result;
    }



    //更新用户回复数(添加)
    public function inc_dcomment_num_for_fellow($user_id)
    {
        $this->db->set('dcomment_count', 'dcomment_count+1', FALSE);
        $this->db->where(array("id"=>(int)$user_id));
        $result = $this->db->update("ml_fellow");
        return $result;
    }

    /**
     * 更新帖子 回复数 (添加)
     * @param $post_id
     * @return object
     */
    public function inc_dcomment_num_for_disubject($did)
    {
        // 更新rss redis缓存
        $this->load->model('redism');
        $this->redism->zset_incr_comment_total($did, 1);

        $key = disubjectm::GET_ONE_DISUBJECT.$did;
        $res = $this->redis->hIncrBy($key, 'comment_total', 1);

        $result = '';
        if ($res) {
            $this->db->set('dcomment_count', 'dcomment_count+1', FALSE);
            $this->db->set('lastcomment', time());
            $this->db->where(array("id"=>(int)$did));
        }
        $result = $this->db->update("ml_disubject");
        return $result;
    }



}
